<?php

namespace App\Admin\Forms;

use App\Models\System;
use Encore\Admin\Widgets\Form;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\Facades\Image;

class ViewSetting extends Form
{
    /**
     * The form title.
     *
     * @var string
     */
    public $title = '页面设置';

    /**
     * Handle the form request.
     *
     * @param Request $request3
     *
     * @return \Illuminate\Http\RedirectResponse
     */
    public function handle(Request $request)
    {
        if(!empty($request->file('banner'))){

            $fileName = 'banner/'.date("YmdHis",time()).rand(1000,9999).'.jpg';

            $upload = Image::make($request->file('banner'))->resize(1920, 900)->save('uploads/'.$fileName);

            if ($upload) {
                $system = System::where('id' , 999)->first();

                $system->banner = $fileName;
                if($system->save()){
                    // 更新当前文件名称  //格式：域名+filesystems.php设置的文件位置+新文件名称
                    admin_success('保存成功');
                }else{
                    admin_error('上传发生错误');
                }
            }else{
                admin_error('错上传发生错误误');
            }
        }

        return back();
    }

    /**
     * Build a form here.
     */
    public function form()
    {
        $this->image('banner' , __('首页图'))->help('建议尺寸1920 * 900');
    }

    /**
     * The data of the form.
     *
     * @return array $data
     */
    public function data()
    {
        return [
            'banner'  => System::where('id' , 999)->value('banner'),
        ];
    }
}
